import 'package:flutter/widgets.dart';
import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/ext.dart';
import 'package:safetymonitor/app/core/http/http.dart';
import 'package:safetymonitor/app/core/utils/file_util.dart';
import 'package:safetymonitor/app/core/utils/interface.dart';
import 'package:safetymonitor/app/data/entities/home/knownledge/knowledge_base_info_entity.dart';
import 'package:url_launcher/url_launcher.dart';

part 'iface.dart';

const _textColor = Color(0xff2F3652);

final class KnowledgeBaseDetailPage extends StatefulWidget {
  KnowledgeBaseDetailPage({super.key});

  final entity = Get.arguments as KnowledgeBaseInfoEntity;

  @override
  State<KnowledgeBaseDetailPage> createState() => _KnowledgeBaseDetailPageState();
}

final class _KnowledgeBaseDetailPageState extends State<KnowledgeBaseDetailPage> {
  KnowledgeBaseInfoEntity get entity => widget.entity;

  @override
  void initState() {
    _fecthListDe(entity.id);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('知识库', style: TextStyle(fontSize: 16.w)),
        backgroundColor: Colors.white,
        centerTitle: true,
      ),
      backgroundColor: Colors.white,
      body: ValueListenableBuilder(
          valueListenable: getData,
          builder: (_, data, __) {
            return _buildBody(data);
          }),
    );
  }

  Widget _buildBody(KnowledgeBaseInfoEntity? data) {
    //2024/12/12 12:00·制度类型名称·所属公司·所属部门·人数
    final time = data?.createTime?.millTS2Str() ?? '未知时间';
    final attr = data?.attributeName ?? '未知属性';
    final dept = data?.deptName ?? '未知部门';
    final person = data?.personIntro == null ? '未知人员' : data?.personIntro?.split(',').last;
    return ListView(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 10.w),
      children: [
        AppGaps.vGap10,
        Text(
          data?.fileName ?? '未知文件名',
          style: TextStyle(
            fontSize: 14.w,
            fontWeight: FontWeight.w500,
            color: _textColor,
          ),
        ),
        AppGaps.vGap10,
        Text(
          '$time·$attr·$dept·$person',
          style: const TextStyle(
            color: Colors.grey,
          ),
        ),
        AppGaps.vGap10,
        const Divider(
          color: Color(0xffEEF0F7),
        ),
        AppGaps.vGap10,
        HtmlWidget(
          //
          data?.fileContent ?? '内容为空',
          onTapUrl: (url) => launchUrl(Uri.parse(url)),

          //选择HTML正文的呈现模式
          // //默认情况下，呈现一个简单的“列”
          // //考虑使用“ListView”或“SliverList”以获得更好的性能
          renderMode: RenderMode.column,
          // 设置文本的默认样式
          textStyle: TextStyle(
            fontSize: 14.w,
            color: _textColor,
          ),
        ),
        // Text(
        //   entity.fileContent ?? '内容为空',
        //   style: TextStyle(
        //     fontSize: 14.w,
        //     color: _textColor,
        //   ),
        // ).clickOnce(() {
        //   print(' entity.fileContent ${ entity.fileContent }');
        // }),
        AppGaps.vGap10,
        if (data?.fileUrl == null || data!.fileUrl!.isEmpty)
          Container()
        else
          Column(
            children: data.fileUrl!
                .split(',')
                .map((e) => Container(
                      margin: EdgeInsets.only(bottom: 5.w),
                      decoration: BoxDecoration(
                        color: const Color(0xffF4F6FC),
                        borderRadius: BorderRadius.circular(7.w),
                      ),
                      padding: EdgeInsets.symmetric(horizontal: 17.w, vertical: 23.w),
                      child: Row(
                        children: [
                          FileUtils.getIcon(e, width: 19.w, height: 19.w),
                          AppGaps.hGap10,
                          Expanded(
                            child: Text(
                              // data?.fileType ?? '未知类型',
                              e.toString().substring(25),
                              style: TextStyle(
                                color: _textColor,
                                fontSize: 14.w,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ).clickOnce(() {
                      FileUtils.openFile(e);
                    }))
                .toList(),
          ),
      ],
    );
  }
}
